#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){
	ofSetWindowTitle("Bamboo - TUIO");
	//ofSetFrameRate(30);

	verboseMode = false;

	server.start("default", 0);
	server.tablet = &bamboo;

	ofAddListener(bamboo.fingerDown, this, &testApp::fingerDown);
	ofAddListener(bamboo.fingerMoved, this, &testApp::fingerMoved);
	ofAddListener(bamboo.fingerUp, this, &testApp::fingerUp);
	if(bamboo.fingersFound) {
		printHelp();
	}
}

//--------------------------------------------------------------
void testApp::update(){
	if(bamboo.fingersFound) {
		bamboo.update();
		server.run();
	}
}

//--------------------------------------------------------------
void testApp::draw(){
	server.drawCursors();
}

//--------------------------------------------------------------
void testApp::keyPressed(int key){
	switch(key) {
		case 'v':
			verboseMode = !verboseMode;
			break;
		case 'r':
			bamboo.findFingers();
			break;
	}
}

//--------------------------------------------------------------
void testApp::keyReleased(int key){

}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::windowResized(int w, int h){

}

void testApp::fingerDown(Finger &finger) {
	finger.cursor = server.addCursor((float)finger.x, (float)finger.y);
	if(verboseMode) {
		printf("finger %d down [%d, %d]\n", finger.fingerId, finger.x, finger.y);
	}
}

void testApp::fingerMoved(Finger &finger) {
	if (finger.cursor == NULL) return;
	if (finger.cursor->getTuioTime() == server.getCurrentTime()) return;
	server.updateCursor(finger.cursor, (float)finger.x, (float)finger.y);
	if(verboseMode) {
		printf("finger %d moved [%d, %d]\n", finger.fingerId, finger.x, finger.y);
	}
}

void testApp::fingerUp(Finger &finger) {
	if (finger.cursor==NULL) return;

	server.removeCursor(finger.cursor);
	finger.cursor = NULL;

	if(verboseMode) {
		printf("finger %d up\n", finger.fingerId);
	}
}

void testApp::printHelp() {
	printf("v - Toggle Verbose Mode\n");
}

